package com.example.demo.jpa.service;

import com.example.demo.jpa.mapper.JPARepository;
import com.example.demo.model.Material;
import com.example.demo.model.QMaterial;
import com.example.demo.model.vo.MaterialAddVo;
import com.example.demo.model.vo.MaterialSelectVo;
import com.example.demo.model.vo.MaterialUpdateVo;
import com.example.demo.model.vo.MaterialVo;
import com.example.demo.util.AllRecords;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import com.example.demo.util.StatusNameEnum;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by 86151 on 2018/12/25.
 */
@Service
public class JpaServiceImpl implements JpaService {

    @Autowired
    private JPARepository jpaRepository;

    @Override
    public Result<AllRecords> queryPage(MaterialSelectVo materialSelectVo) {
        BooleanExpression booleanExpression = Expressions.asBoolean(true).isTrue();
        if (!StringUtils.isEmpty(materialSelectVo.getMaterialCode())){
            booleanExpression =  booleanExpression.and(QMaterial.material.materialCode.eq(materialSelectVo.getMaterialCode()));
        }
        if (!StringUtils.isEmpty(materialSelectVo.getMaterialName())){
            booleanExpression =  booleanExpression.and(QMaterial.material.materialName.eq(materialSelectVo.getMaterialName()));
        }
        //1.查询条件，2.索引和每页的大小，3.排序根据某个字段进行排序
        Page<Material> plantPage = jpaRepository.findAll(booleanExpression, new PageRequest(materialSelectVo.getOffset(), materialSelectVo.getPageSize(), new Sort(Sort.Direction.DESC, "id")));
        System.out.println(plantPage.getContent());
          AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(materialSelectVo.getPageIndex());
        allRecords.setPageSize(materialSelectVo.getPageSize());
        allRecords.setTotalNumber(plantPage.getNumber());
        allRecords.setDataList(plantPage.getContent());
        allRecords.resetTotalNumber(plantPage.getNumber());
        return ResultVo.success(allRecords);
    }

    @Override
    public Result<MaterialAddVo> addMaterialJpa(MaterialAddVo materialAddVo) {
        Date nowDate = new Date();
        Material material = new Material();
        material.setCreateId(1);
        material.setCreateName("小明");
        material.setCreateTime(nowDate);
        material.setUpdateId(1);
        material.setUpdateName("小明");
        material.setUpdateTime(nowDate);
        BeanUtils.copyProperties(materialAddVo,material);
        //验证逻辑：备件编码不能重复
        List<Material> byMaterialCode = jpaRepository.findByMaterialCode(material.getMaterialCode());
        if (!CollectionUtils.isEmpty(byMaterialCode)){
            return ResultVo.error("500","备件编码已经存在！");
        }
        //验证逻辑：备件名称不能重复
        List<Material> byMaterialName = jpaRepository.findByMaterialName(material.getMaterialName());
        if (!CollectionUtils.isEmpty(byMaterialName)){
            return ResultVo.error("500","备件名称已经存在！");
        }
        jpaRepository.save(material);
        return ResultVo.success(materialAddVo);
    }

    @Override
    public Result<MaterialVo> queryById(Integer id) {
        MaterialVo materialVo = new MaterialVo();
        Material material = jpaRepository.findOne(id);
        if (material==null){
            return ResultVo.error("500","查询对象不存在！");
        }
        BeanUtils.copyProperties(material,materialVo);
        if (material.getStatus()){
            materialVo.setStatusName(StatusNameEnum.QIYONG.getStatus());
        }else {
            materialVo.setStatusName(StatusNameEnum.JINYONG.getStatus());
        }
        return ResultVo.success(materialVo);
    }

    @Override
    public Result<MaterialUpdateVo> updateMaterial(MaterialUpdateVo materialUpdateVo) {
        //验证要修改的数据是否存在
        Material material = jpaRepository.findOne(materialUpdateVo.getId());
        if (material==null){
            return ResultVo.error("500","修改对象不存在！");
        }
        //验证修改后的名称是否已经存在
        List<Material> byMaterialName = jpaRepository.findByMaterialName(materialUpdateVo.getMaterialName());
        if (!CollectionUtils.isEmpty(byMaterialName)){
            return ResultVo.error("500","备件名称已经存在！");
        }
//        Material material1 = new Material();
        material.setUpdateId(1);
        material.setUpdateName("小明");
        material.setUpdateTime(new Date());
        //操作数据库
        BeanUtils.copyProperties(materialUpdateVo,material);
         jpaRepository.save(material);
         return ResultVo.success(materialUpdateVo);
    }
}
